<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>

    <title>OnlineMusic</title>
    <link rel="shortcut icon" href="#"/>

    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.0/css/bootstrap.min.css" rel="stylesheet">
    <script src="js/jquery-3.1.1.min.js"></script>
    <link href="css/bootstrap.min.css" rel="stylesheet">

    <style type="text/css">
        body {
            font-family: Arial, sans-serif;
            background-color: #cbdee7;
            color: #fff;
            margin: 0;
            padding: 0;
        }

        .container {
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #7db2e7;
            border-radius: 5px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
        }

        h3 {
            text-align: center;
            margin-bottom: 30px;
            color: #ffffff;
        }

        form {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
        }

        table {
            width: 100%;
            border-collapse: collapse;
        }

        .table1 {
            height: 450px; /* 设置固定高度 */
            overflow: auto; /* 设置滚动条 */
        }

        th, td {
            padding: 8px;
            text-align: center;
        }

        th {
            background-color: #167ae0;
            color: #ffc107;
            font-weight: bold;
        }

        tbody tr:nth-child(even) {
            background-color: #ef4747;
        }

        .btn {
            margin-right: 5px;
        }

        .btn-primary {
            background-color: #131312;
            border-color: #ffffff;
        }

        .btn-primary:hover {
            background-color: #d5c69a;
            border-color: #ffffff;
        }
    </style>

    <script type="text/javascript">
        // 核心代码实现
        $(function(){
            load();
        });
        //musicName可以传参  也可以不传参数
        function load(musicName) {

            $.ajax({
                url:"/music/findmusic",
                data:{"musicName": musicName},
                type:"GET",
                dataType:"json",
                success:function(obj) {
                    console.log(obj);
                    var data = obj.data;//数组
                    var s = '';
                    //data[i].id    data[i].singer  data[i].title
                    for(var i = 0; i < data.length;i++) {
                        var musicUrl = data[i].url;
                        console.log(musicUrl);
                        s += '<tr>';
                        s += '<th > <input id= "'+data[i].id+'" type="checkbox"> </th>';
                        s += '<td>'+data[i].title+'</td>';
                        s += '<td>'+data[i].singer+'</td>';
                        s += '<td> <button class = "btn btn-primary"  onclick="playerSong(\''+musicUrl+'\')"> 播放歌曲 </button>' +'</td>';
                        s += '<td> <button class = "btn btn-primary"  onclick="deleteInfo('+data[i].id+')"> 删除 </button>' +
                            '<button class = "btn btn-primary"  onclick="loveInfo('+data[i].id+')"> 喜欢 </button>' +'</td>';
                        s += "</tr>"
                    }
                    $("#info").html(s);
                }
            });
        }
        function playerSong(obj) {
            console.log("obj = "+obj);
            var name = obj.substring(obj.lastIndexOf("=")+1 ,obj.lastIndexOf("."));
            //obj:播放的音乐的地址  name:播放的音乐的名称，0：播放的开始时间  false:不自动播放
            SewisePlayer.toPlay(obj,name,0,true);
        }

        function deleteInfo(obj) {
            var b;
            b = confirm( "请你确定删除操作" ); //在页面上弹出对话框
            if (b == true ){
                $.ajax({
                    url:"/music/delete",
                    type: "POST",
                    data:{"id":obj},
                    dataType:"json",
                    success: function(val) {
                        console.log(val);
                        if(val.data == true) {
                            //删除成功！
                            alert("删除成功！");
                            window.location.href = "list.html";
                        }else{
                            alert("删除失败！");
                        }
                    }
                });
            }else{
                return;
            }
        }

        $(function() {
            $("#submit1").click(function () {
                var name = $("#exampleInputName2").val();
                load(name);
            });
            $.when(load).done(function(){
                $("#delete").click(function(){
                    var ids = new Array();
                    var i = 0;//数组的小标
                    //
                    $("input:checkbox").each(function(){
                        //如果被选中,this代表发生事件的dom元素，<input>
                        if( $(this).is(":checked")) {
                            ids[i] = $(this).attr("id");
                            i++;
                        }
                    });

                    console.log(ids);
                    var a;
                    a = confirm( "确定删除选中的音乐?" ); //在页面上弹出对话框
                    if (a == true ){
                        if(i == 0){
                            alert("未选中要删除的音乐");
                            return;
                        }
                        $.ajax({
                            url:"/music/batchDelete",
                            data:{"ids":ids},
                            dataType:"json",
                            type:"post",

                            success:function(obj){
                                if(obj.data == true) {
                                    alert("删除成功！");
                                    window.location.href = "list.html";
                                }else{
                                    alert("删除失败！");
                                }
                            }
                        });
                    }else{
                        return;
                    }

                });
            });
        });
        function loveInfo(obj) {

            $.ajax({
                url:"/lovemusic/likemusic",
                type:"POST",
                data:{"id":obj},
                dataType:"json",

                success:function(val){
                    if(val.data == true) {
                        alert("收藏成功！");
                        window.location.href = "list.html";
                    }else{
                        alert("收藏失败！");
                    }
                }
            });
        }

    </script>
</head>

<body>
<div class="container">
    <h3>音乐播放器</h3>

<!--    <div >-->
<!--        <form>-->
<!--            <div class="form-group">-->
<!--                <label for="exampleInputName2">歌曲名</label>-->
<!--                <input name="name" type="text" class="form-control" id="exampleInputName2">-->
<!--            </div>-->
<!--            <button  id="submit1" type="button" class="btn btn-primary" style="float: left;">查询</button>-->
<!--        </form>-->
<!--    </div>-->
    <div style="float: left;">
        <form class="form-inline">
            <div class="form-group">
                <label for="exampleInputName2">歌曲名</label>
                <input name="name" type="text" class="form-control" id="exampleInputName2" >
            </div>
            <button id="submit1" type="button" class="btn btn-primary">查询</button>
        </form>
    </div>
    <div style="clear: both;"></div>

    <div style="margin-bottom: 15px;" class="form-group">
        <a class="btn btn-primary" href="loveMusic.html">喜欢列表</a>
        <a class="btn btn-primary" href="upload.html">添加歌曲</a>
        <a id="delete" class="btn btn-primary">删除选中</a>
    </div>
    <div class="table1">
        <table class="table table-bordered table-hover">
            <thead>
            <tr>
                <th>选择</th>
                <th>歌名</th>
                <th>歌手</th>
                <th>歌曲</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody id="info">
            <tr>
                <td><input id="1" type="checkbox"></td>
                <td>成都</td>
                <td>赵雷</td>
                <td>
                    <button class="btn btn-primary">播放歌曲</button>
                </td>
                <td>
                    <button class="btn btn-primary">删除</button>
                    <button class="btn btn-primary">喜欢</button>
                </td>
            </tr>
            </tbody>
        </table>
    </div>

</div>
</body>

<div style="width: 180px; height: 140px; position:absolute; bottom:10px; right:10px">
    <script type="text/javascript" src="player/sewise.player.min.js"></script>
    <script type="text/javascript">
        SewisePlayer.setup({
            server: "vod",
            type: "mp3",
            //这里是默认的一个网址
            videourl:"/music/热烈的少年.mp3",
            skin: "vodWhite",
            //这里需要设置false
            autostart:"false",
        });
    </script>
</div>

</html>
